Better typings for Promise, like #31117 - #33707
Conversation
c2c7c0a to
c0fdc66
Compare
|
TypeScript Bot (@typescript-bot) test this |
9e9dd9c to
497d816
Compare
| * @returns A new Promise. | ||
| */ | ||
| race<T>(values: readonly T[]): Promise<T extends PromiseLike<infer U> ? U : T>; | ||
| all<T extends readonly any[]>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>; |
There was a problem hiding this comment.
Given this overload, are the tuple-based overloads even necessary?
There was a problem hiding this comment.
You're right! I've now updated this PR accordingly. Thank you!
7224336 to
ba2ffbc
Compare
|
TypeScript Bot (@typescript-bot) test this |
|
Heya Orta Therox (@orta), I've started to run the extended test suite on this PR at ba2ffbc. You can monitor the build here. It should now contribute to this PR's status checks. |
|
TypeScript Bot (@typescript-bot) user test this |
ba2ffbc to
803e351
Compare
|
Ron Buckton (@rbuckton) can you please review and merge if this is good to go? |
500160b to
16ded29
Compare
|
Ron Buckton (@rbuckton) or Ryan Cavanaugh (@RyanCavanaugh) Can you please trigger TypeScript Bot (@typescript-bot) test this, run dt and user test this for me? |
16ded29 to
e35ba5c
Compare
e35ba5c to
907a6ff
Compare
| * @param values An array of Promises. | ||
| * @returns A new Promise. | ||
| */ | ||
| race<T>(values: Iterable<T | PromiseLike<T>>): Promise<T>; |
There was a problem hiding this comment.
It existed here so that you could include es2015.promise without es2015.iterable, depending on what shims/polyfills are used down-level, although it looks like a definition including Iterable was incorrectly added to that file at some point.
There was a problem hiding this comment.
I'm happy to fix that but just wondering, is it better to be able to include es2015.promise without es2015.iterable, or es2015.iterable without es2015.promise? The latter is more obvious (all Promise.all() and Promise.race() definitions located together) and has fewer overloads (since readonly T[] extends Iterable<T>).
There was a problem hiding this comment.
Sorry, I get it now: es2015.iterable doesn't depend on PromiseConstructor, it augments it. I'll move that Iterable overload back to es2015.iterable.
There was a problem hiding this comment.
✔️ Done.
907a6ff to
52bc68c
Compare
57ad578 to
9cdcb89
Compare
9cdcb89 to
d2aff9c
Compare
d2aff9c to
5262a70
Compare
You can include es2015.promise.d.ts without es2015.iterable.d.ts. It was moved to es2015.promise.d.ts in microsoft#31117
5262a70 to
ab5117a
Compare
|
as in my issue comment in #37526 UpdateMy problem is solved with the nightly version of typescript but I will leave it there in case it can be useful for something or someone
I have placed a copy of this resolved problem in #33055 and in #33707 hoping it can be helpful and not too spammy ... Issue now fixedI have a similar problem and I don't know if I should open a different issue ...
// Promise.all(this.fork) take a T1[] where T1 is a Promise<number>
// and return a Promise<T2[]> where T2 is a number
// therefore T1 and T2 are not both «T»export class MyMaybeList<T = any> {
private constructor(values: T[]) {
this.values = values;
}
private values: T[];
// get =======================================================-| fork() |-====
public get fork(): T[] {
return this.values != null ? this.values.slice() : [].slice();
}
// public =====================================================-| map() |-====
public map<R = any>(
fn: (val: T, index: number, array: T[]) => R
): MyMaybeList<R> {
return MyMaybeList.of(...this.values.map(fn));
}
// static ======================================================-| of() |-====
public static of<TVal>(...val: TVal[]): MyMaybeList<TVal> {
if (val == null || !val.length) return new MyMaybeList<TVal>([]);
return new MyMaybeList<TVal>([...val]);
}
// async =====================================================-| will() |-====
public async will /* <R> */() /* : Promise<MyMaybeList<R>> */ {
// Promise.all(this.fork) take a T1[] where T1 is Promise<number>
// and return a Promise<T2[]> where T2 is a number
// therefore T1 and T2 are not both «T»
console.log(this.fork);
const willThen = Promise.all(this.fork);
const thenWill = await willThen;
return MyMaybeList.of(...thenWill);
}
}const oneToTen = MyMaybeList.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
const powersOneToTen = oneToTen.map(async val => val * val);
// "log->" powersOneToTen: MyMaybeList<Promise<number>>
console.log(powersOneToTen);
// "log->" awaitedList: MyMaybeList<Promise<number>>
// instead of a -> MyMaybeList { Promise<number> }
// in fact is a -> Promise { MyMaybeList<number> }
const awaitedPowersOneToTen = powersOneToTen.will /* <unnknow> */();
awaitedPowersOneToTen.then(awaitedList => console.log(awaitedList));
// Promise { <pending> } will resolve into ->
console.log(awaitedPowersOneToTen);/*
// Promise.all(this.fork) take a T1[] where T1 is Promise<number>
// and return a Promise<T2[]> where T2 is a number
// therefore T1 and T2 are not both «T»
// console.log(this.fork); will display same as console.log(powersOneToTen);
// console.log(powersOneToTen); ->
// "log->" powersOneToTen: MyMaybeList<Promise<number>>
MyMaybeList {
values: [
Promise { 1 },
Promise { 4 },
Promise { 9 },
Promise { 16 },
Promise { 25 },
Promise { 36 },
Promise { 49 },
Promise { 64 },
Promise { 81 },
Promise { 100 }
]
}
// console.log(awaitedPowersOneToTen); ->
// "log ->" awaitedPowersOneToTen: Promise<MyMaybeList<Promise<number>>>
Promise { <pending> }
// Promise { <pending> } will resolve into ->
// awaitedPowersOneToTen.then(awaitedList => console.log(awaitedList)); ->
// console.log(awaitedList) ->
// "log->" awaitedList: MyMaybeList<Promise<number>>
// instead of a -> MyMaybeList { Promise<number> }
// in fact is a (should be) -> Promise { MyMaybeList<number> }
MyMaybeList {
values: [
1, 4, 9, 16, 25,
36, 49, 64, 81, 100
]
}
*/ |
|
Now that #37610 has reverted the
|
|
Sorry to be that guy but what is the status of this PR? The regression in #33752 is super annoying. 😞 |
|
Valentin Hervieu (@ValentinH) The regression in #33752 should be now fixed with #34501, it will be released with 3.9 on May, 12. |
|
Grigory Streltsov (@strelga) thank you really much, this is good news. Still I found the new definition for |
|
All of the issues this PR links are closed; I think that this all was handled in 4.5 with I'm going to close this, but am happy to reopen if I'm mistaken. |



Roll up #33055,
#33062,#33074,#33103and#33105Fixes
#27711Fixes #22469
Fixes #28427
Fixes
#30390Fixes #31722
Fixes #33559
Fixes #33562
Fixes #33752
Fixes
#34924Fixes #34937
Fixes
#35136Fixes
#35258